import {
  createRouter,
  createWebHistory,
  RouteLocationNormalized,
  RouteRecordRaw,
} from "vue-router";
import cache from "@/utils/cache";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/main",
  },
  {
    path: "/main",
    component: () => import("@/views/main/mainPage.vue"),
    children: [],
  },
  {
    path: "/login",
    component: () => import("@/views/login/loginPage.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    name: "not-found",
    component: () => import("@/views/not-found/not-found.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});
router.beforeEach((to: RouteLocationNormalized) => {
  if (to.path !== "/login") {
    if (!cache.getCache("token")) {
      return "/login";
    }
  }
});
export default router;
